﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoffeeMobile.Models.ViewModels;
using System.IO;
using CoffeeMobile.Models;
using System.Collections.ObjectModel;

namespace CoffeeMobile.Controllers
{
    public class FoodsController : BaseController
    {
        public ActionResult Edit(Guid foodid)
        {
            AddEditNewFoodModel model = _dbLinq.Foods.Where(p => p.ID == foodid).Select(p => new AddEditNewFoodModel
            {
                ID = p.ID,
                Name = p.Name,
                Description = p.Description,
                GroupName = _dbLinq.FoodGroups.Where(g => p.ID == p.FoodGroupID).FirstOrDefault().Name,
                Image = p.Image,
                UnitPrice = (int)p.UnitPrice,
                FoodGroupID =(Guid) p.FoodGroupID
            }).FirstOrDefault();
            ViewBag.FoodsGroupName = _dbLinq.FoodGroups.Where(p => p.ID == model.FoodGroupID).FirstOrDefault().Name;
            ViewBag.FoodGroups = _dbLinq.FoodGroups.Select(p => new { GroupID = p.ID, Name = p.Name }).ToList();
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
           Food entity = _dbLinq.Foods.Where(p => p.ID == id).FirstOrDefault();
            try
            {
                if (Request.Files.Count > 0)
                {
                    foreach (string name in Request.Files)
                    {
                        HttpPostedFileBase file = Request.Files[name];
                        if (file.ContentLength > 0)
                        {
                            string fileName = Path.GetFileName(file.FileName);
                            string directory = Server.MapPath("~/Content/img/");
                            if (!Directory.Exists(directory))
                            {
                                Directory.CreateDirectory(directory);
                            }
                            string path = Path.Combine(directory, fileName);
                            file.SaveAs(path);
                            entity.Image = file.FileName;
                        }
                        
                    }
                }
                entity.Name=collection["Name"];
                entity.Description = collection["Description"];
                entity.IsDeleted = false;
                _dbLinq.SubmitChanges();

                ViewBag.Message = "Thêm thức ăn/ nước uống thành công!";
                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.FoodGroups = _dbLinq.FoodGroups.Select(p => new { GroupID = p.ID, Name = p.Name }).ToList();
                ViewBag.FoodsGroupName = _dbLinq.FoodGroups.Where(p => p.ID == Guid.Parse(collection["FoodGroupID"])).FirstOrDefault().Name;
                ViewBag.Message = "Thêm thức ăn/ nước uống thất bại!";
                return View(entity);
            }
        }
        //
        // GET: /Foods/

        public ActionResult Index()
        {
            return View(GetFoodGroups());
        }
        public List<FoodGroupModel> GetFoodGroups()
        {
            List<FoodGroupModel> list = (from p in _dbLinq.FoodGroups
                                         select new FoodGroupModel { ID = p.ID, Name = p.Name }).ToList();
            foreach (FoodGroupModel item in list)
            {
                item.FoodModels = (from p in _dbLinq.Foods
                                   where p.FoodGroupID == item.ID && p.IsDeleted == false
                                   select new FoodModel
                                   {
                                       ID = p.ID,
                                       Name = p.Name,
                                       Image = p.Image,
                                       Description = p.Description
                                   }).ToList();
            }
            return list;
        }
        public ActionResult Details()
        {
            ViewBag.Foods = GetFoodGroups();
            return View();
        }

        public ActionResult Create()
        {
            List<FoodGroupModel> list = (from p in _dbLinq.FoodGroups
                                         select new FoodGroupModel { ID = p.ID, Name = p.Name }).ToList();
            foreach (FoodGroupModel item in list)
            {
                item.FoodModels = (from p in _dbLinq.Foods
                                   where p.FoodGroupID == item.ID
                                   select new FoodModel
                                   {
                                       ID = p.ID,
                                       Name = p.Name
                                   }).ToList();
            }
            ViewBag.Foods = list;
            ViewBag.FoodGroups = _dbLinq.FoodGroups.Select(p => new { GroupID = p.ID, Name = p.Name }).ToList();

            return View();
        }
        //, HttpPostedFileBase Picture
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(AddEditNewFoodModel model)
        {

            try
            {
                Food entity = new Food();
                entity.ID = Guid.NewGuid();
                entity.Name = model.Name;
                entity.Description = model.Description;
                if (Request.Files.Count > 0)
                {
                    foreach (string name in Request.Files)
                    {
                        HttpPostedFileBase file = Request.Files[name];
                        if (file.ContentLength > 0)
                        {
                            string fileName = Path.GetFileName(file.FileName);
                            string directory = Server.MapPath("~/Content/img/");
                            if (!Directory.Exists(directory))
                            {
                                Directory.CreateDirectory(directory);
                            }
                            string path = Path.Combine(directory, fileName);
                            file.SaveAs(path);
                        }
                        entity.Image = file.FileName;
                    }
                }
                entity.FoodGroupID = model.FoodGroupID;
                entity.Description = model.Description;
                entity.UnitPrice = model.UnitPrice;
                entity.IsDeleted = false;
                _dbLinq.Foods.InsertOnSubmit(entity);
                _dbLinq.SubmitChanges();

                ViewBag.Message = "Thêm thức ăn/ nước uống thành công!";
                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.FoodGroups = _dbLinq.FoodGroups.Select(p => new { GroupID = p.ID, Name = p.Name }).ToList();
                ViewBag.Message = "Thêm thức ăn/ nước uống thất bại!";
                return View();
            }
        }
        public ActionResult Delete(Guid foodid)
        {
            DeleteFoodModel model = _dbLinq.Foods.Where(p => p.ID == foodid).Select(p => new DeleteFoodModel
            {
                ID = p.ID,
                Name = p.Name,
                Description = p.Description,
                GroupName = _dbLinq.FoodGroups.Where(g => p.ID == p.FoodGroupID).FirstOrDefault().Name,
                Image = p.Image,
                UnitPrice = (int)p.UnitPrice,
                FoodGroupID =(Guid) p.FoodGroupID
            }).FirstOrDefault();
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(DeleteFoodModel model)
        {

            try
            {
                Food entity = _dbLinq.Foods.Where(p => p.ID == model.ID).FirstOrDefault();
                entity.IsDeleted = true;
                _dbLinq.SubmitChanges();
            }
            catch
            {
                return View(model);
            }

            return RedirectToAction("Index");
        }
        //public tbfood Convert(DeleteFoodModel entity)
        //{
        //    return new tbfood
        //    {
        //        ID = entity.ID,
        //        Name = entity.Name,
        //        UnitPrice = entity.UnitPrice,
        //        Img = entity.Image,
        //        Description = entity.Description,
        //        FoodGroupID = entity.FoodGroupID,
        //    };
        //}
        //public tbfood Convert(AddEditNewFoodModel entity)
        //{
        //    return new tbfood
        //    {
        //        Name = entity.Name,
        //        UnitPrice = entity.UnitPrice,
        //        //Img = entity.Image,
        //        Description = entity.Description,
        //        FoodGroupID = entity.FoodGroupID,
        //    };
        //}
    }
}
